package com.ctz.commonframe.utils;

import java.io.File;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

public class FilePathUtil {
	public static final String AUDIO_FOLDER = "/Audio/";
	public static final String VIDEO_FOLDER = "/Video/";
	public static final String IMAGE_FOLDER = "/Image/";
	public static final String CACHE_FOLDER = "/Cache/";
	public static final String ATTACHMENT_FOLDER = "/Attachment/";
	public static final String LOG_FOLDER = "/Log/";
	public static final String DOWNLOAD_FOLD = "/Download/";
	
	public static final String PREFIX_IMAGE = "IMAGE_";
	public static final String PREFIX_AUDIO = "AUDIO_";
	public static final String PREFIX_VIDEO = "VIDEO_";
	public static final String PREFIX_CACHE = "CACHE_";
	public static final String PREFIX_LOG = "LOG_";
	
	public static final String SUFFIX_IMAGE = ".jpg";
	public static final String SUFFIX_AUDIO = ".mp3";
	public static final String SUFFIX_TXT = ".txt";

	public static String getExternalRootDir() {
		if(isSdCardEnabled()){
			return Environment.getExternalStorageDirectory().getAbsolutePath();
		}
		return null;
	}

	/**
	 * 获取下载文件的存储目录
	 * @param context
	 * @return
	 */
	public static String getDownloadDir(Context context){
		String dir;
		if (isSdCardEnabled()) {
			dir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
		} else {
			dir = context.getFilesDir().getAbsolutePath();
		}
		File dirFile = new File(dir);
		if (!dirFile.exists()) {
			dirFile.getParentFile().mkdirs();
		}
		return dir;
	}

	/**
	 * 获取图片文件的存储目录
	 * @param context
	 * @return
	 */
	public static String getImageDir(Context context){
		String dir;
		if (isSdCardEnabled()) {
			dir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath();
		} else {
			dir = context.getFilesDir().getAbsolutePath();
		}
		File dirFile = new File(dir);
		if (!dirFile.exists()) {
			dirFile.getParentFile().mkdirs();
		}
		return dir;
	}

	/**
	 * 获取音频文件的存储目录
	 * @param context
	 * @return
	 */
	public static String getAudioDir(Context context){
		String dir;
		if (isSdCardEnabled()) {
			dir = context.getExternalFilesDir(Environment.DIRECTORY_MUSIC).getAbsolutePath();
		} else {
			dir = context.getFilesDir().getAbsolutePath();
		}
		File dirFile = new File(dir);
		if (!dirFile.exists()) {
			dirFile.getParentFile().mkdirs();
		}
		return dir;
	}

	/**
	 * 获取缓存的存储目录
	 * @param context
	 * @return
	 */
	public static String getCacheRootDir(Context context) {
		String cachePath;
		if (isSdCardEnabled()) {
			cachePath = context.getExternalCacheDir().getAbsolutePath();
		} else {
			cachePath = context.getCacheDir().getPath();
		}
		return cachePath;
	}

	/**
	 * 获取图片文件名
	 * @param currentMillis 当前时间毫秒数
	 * @return
	 */
	public static String getImageFileName(long currentMillis){
		return PREFIX_IMAGE + currentMillis + SUFFIX_IMAGE;
	}

	/**
	 * 获取图片文件名
	 * @param currentMillis 当前时间毫秒数
	 * @return
	 */
	public static String getAudioFileName(long currentMillis){
		return PREFIX_AUDIO + currentMillis + SUFFIX_AUDIO;
	}

	/**
	 * 获取LOG文件名
	 * @param currentMillis 当前时间毫秒数
	 * @return
	 */
	public static String getLogFileName(long currentMillis){
		return PREFIX_LOG + currentMillis + SUFFIX_TXT;
	}

	/**
	 * 获取图片缓存文件名
	 *
	 * @return
	 */
	public static String getImageCacheFileName(){
		return "ImageCache";
	}

	private static boolean isSdCardEnabled() {
	    return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
				|| !Environment.isExternalStorageRemovable();
	}
}
